//
//  FindDetailTableViewCell.m
//  Zaker
//
//  Created by dllo on 16/3/12.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "FindDetailTableViewCell.h"

@implementation FindDetailTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.userImg = [[UIImageView alloc]init];
        //        _userImg.backgroundColor = [UIColor redColor];
        [self.contentView addSubview:_userImg];
        
        self.userLable = [[UILabel alloc]init];
        //        _userLable.backgroundColor = [UIColor orangeColor];
        [self.contentView addSubview:_userLable];
        
        self.timeLable = [[UILabel alloc]init];
        //        _timeLable.backgroundColor = [UIColor yellowColor];
        [self.contentView addSubview:_timeLable];
        
        self.contentLable = [[BaseLabel alloc]init];
        //        _contentLable.backgroundColor = [UIColor greenColor];
        [self.contentView addSubview:_contentLable];
        
        self.picture = [[UIImageView alloc]init];
        //        _picture.backgroundColor = [UIColor blueColor];
        [self.contentView addSubview:_picture];
        
        self.hotImg = [[UIImageView alloc]init];
        //        _hotImg.backgroundColor = [UIColor magentaColor];
        [self.contentView addSubview:_hotImg];
        
        self.hotLabel = [[UILabel alloc]init];
        //        _hotLabel.backgroundColor = [UIColor cyanColor];
        [self.contentView addSubview:_hotLabel];
        
        self.messageImg = [[UIImageView alloc]init];
        //        _messageImg.backgroundColor = [UIColor grayColor];
        [self.contentView addSubview:_messageImg];
        
        self.messageLable = [[UILabel alloc]init];
        //        _messageLable.backgroundColor = [UIColor blackColor];
        [self.contentView addSubview:_messageLable];
        
        self.likeImg = [[UIImageView alloc]init];
        //        _likeImg.backgroundColor = [UIColor purpleColor];
        [self.contentView addSubview:_likeImg];
        
        self.likeLable = [[UILabel alloc]init];
        //        _likeLable.backgroundColor = [UIColor lightGrayColor];
        [self.contentView addSubview:_likeLable];
    }
    return self;
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    _userImg.frame = CGRectMake(5, 10, self.contentView.frame.size.width / 10, self.contentView.frame.size.width / 10);
    _userImg.layer.cornerRadius = self.contentView.frame.size.width / 18;
    _userImg.clipsToBounds = YES;
    
    _userLable.frame = CGRectMake(_userImg.frame.origin.x + _userImg.frame.size.width + 5, _userImg.frame.origin.y, self.contentView.frame.size.width / 2, _userImg.frame.size.height / 3 * 2);
    _userLable.font = [UIFont systemFontOfSize:16];
    _userLable.textColor = [UIColor blueColor];
    
    _timeLable.frame = CGRectMake(_userLable.frame.origin.x, _userLable.frame.origin.y + _userLable.frame.size.height + 2, _userLable.frame.size.width, _userImg.frame.size.height / 3);
    _timeLable.font = [UIFont systemFontOfSize:12];
    _timeLable.textColor = [UIColor grayColor];
    
    //问题内容 需要自定义高度
    CGFloat H = [[self class] heightWithText:_content];
    _contentLable.frame = CGRectMake(_userImg.frame.origin.x, _userImg.frame.origin.y + _userImg.frame.size.height + 10, self.contentView.frame.size.width - 10, H);
    _contentLable.numberOfLines = 0;
    _contentLable.font = [UIFont systemFontOfSize:15];
    
    //自定义图片高度
    CGFloat pictureH = 0;
    if (_isHave) {
        pictureH = [[self class]heightWithImage:self.width WithHeight:self.height];
        if (pictureH < 450) {
            pictureH = pictureH;
        } else {
            pictureH = 350;
            _picture.image = [[self class] getSubImage:_picture.image mCGRect:CGRectMake(0, 0, SCREEN_WIDTH, pictureH) centerBool:YES];
        }
    }else{
        pictureH = 0;
    }
//    if (_isHave) {
//        pictureH = [[self class]heightWithImage:self.width WithHeight:self.height];
//    }
    _picture.frame = CGRectMake(0, _contentLable.frame.origin.y + _contentLable.frame.size.height + 5, self.contentView.frame.size.width, pictureH);
    
    _hotImg.frame = CGRectMake(20, _picture.frame.origin.y + _picture.frame.size.height + 10, 20, 20);
    _hotImg.image = [UIImage imageNamed:@"iconfont-hot"];
    
    _hotLabel.frame = CGRectMake(_hotImg.frame.origin.x + _hotImg.frame.size.width + 5, _hotImg.frame.origin.y, 50, 20);
    _hotLabel.textColor = [UIColor grayColor];
    
    _messageImg.frame = CGRectMake(self.contentView.frame.size.width / 2 - 10, _hotImg.frame.origin.y, _hotImg.frame.size.width, _hotImg.frame.size.height);
    _messageImg.image = [UIImage imageNamed:@"iconfont-message"];
    
    _messageLable.frame = CGRectMake(_messageImg.frame.origin.x + _messageImg.frame.size.width + 5, _messageImg.frame.origin.y, 50, 20);
    _messageLable.textColor = [UIColor grayColor];
    
    _likeImg.frame = CGRectMake(self.contentView.frame.size.width / 7 * 6, _hotImg.frame.origin.y, 20, 20);
    _likeImg.image = [UIImage imageNamed:@"iconfont-zan"];
    
    _likeLable.frame = CGRectMake(_likeImg.frame.origin.x + _likeImg.frame.size.width + 5, _likeImg.frame.origin.y, 50, 20);
    _likeLable.textColor = [UIColor grayColor];
}

/**
 * 截取部分图像
 *
 **/
+(UIImage*)getSubImage:(UIImage *)image mCGRect:(CGRect)mCGRect centerBool:(BOOL)centerBool
{
    
    /*如若centerBool为Yes则是由中心点取mCGRect范围的图片*/
    
    
    float imgwidth = image.size.width;
    float imgheight = image.size.height;
    float viewwidth = mCGRect.size.width;
    float viewheight = mCGRect.size.height;
    CGRect rect;
    if(centerBool)
        rect = CGRectMake((imgwidth-viewwidth)/2, (imgheight-viewheight)/2, viewwidth, viewheight);
    else{
        if (viewheight < viewwidth) {
            if (imgwidth <= imgheight) {
                rect = CGRectMake(0, 0, imgwidth, imgwidth*viewheight/viewwidth);
            }else {
                float width = viewwidth*imgheight/viewheight;
                float x = (imgwidth - width)/2 ;
                if (x > 0) {
                    rect = CGRectMake(x, 0, width, imgheight);
                }else {
                    rect = CGRectMake(0, 0, imgwidth, imgwidth*viewheight/viewwidth);
                }
            }
        }else {
            if (imgwidth <= imgheight) {
                float height = viewheight*imgwidth/viewwidth;
                if (height < imgheight) {
                    rect = CGRectMake(0, 0, imgwidth, height);
                }else {
                    rect = CGRectMake(0, 0, viewwidth*imgheight/viewheight, imgheight);
                }
            }else {
                float width = viewwidth*imgheight/viewheight;
                if (width < imgwidth) {
                    float x = (imgwidth - width)/2 ;
                    rect = CGRectMake(x, 0, width, imgheight);
                }else {
                    rect = CGRectMake(0, 0, imgwidth, imgheight);
                }
            }
        }
    }
    
    CGImageRef subImageRef = CGImageCreateWithImageInRect(image.CGImage, rect);
    CGRect smallBounds = CGRectMake(0, 0, CGImageGetWidth(subImageRef), CGImageGetHeight(subImageRef));
    
    UIGraphicsBeginImageContext(smallBounds.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextDrawImage(context, smallBounds, subImageRef);
    UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];
    UIGraphicsEndImageContext();
    
    return smallImage;
}

//文字高度自适应
+ (CGFloat)heightWithText:(NSString *)text
{
    //在类方法里self代表当前的类
    //写一个字典 保存文本属性(大小)
    NSDictionary *dic = @{NSFontAttributeName: [UIFont systemFontOfSize:17]};
    //预设一个尺寸 表示文本最大不超过这个尺寸
    //根据设置的宽度计算高度(宽度和lable保持一致)
    CGSize size = CGSizeMake(375 - 56 , 1000);
    //根据文本 计算出一个CGRect
    //参数1: 尺寸范围
    //参数2: 文本属性
    //参数3: 按照什么方式获取rect
    //参数4: nil
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    //返回得到的高度
    
    return rect.size.height;
}
//图片高度自适应
+(double)heightWithImage:(NSString *)width WithHeight:(NSString *)height
{
    CGFloat w = width.floatValue;
    CGFloat h = height.floatValue;
    if (w!= 0 && h!= 0) {
        double newh =  (double)(h * 375.0 / w);
        return  newh;
    }else{
        return 0;
    }
    
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
